package org.hhgzs.modules.erp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hhgzs.modules.erp.entity.Unit;
import org.hhgzs.modules.erp.service.UnitService;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * <p>
 * 多单位表 前端控制器
 * </p>
 *
 * @author peiqy
 * @since 2021-07-14
 */
@RestController
@RequestMapping("/unit")
@Slf4j
@RequiredArgsConstructor
@Api("单位管理")
public class UnitController extends JeecgController<Unit, UnitService> {

    private  final UnitService unitService;
    /**
     * 获取用户列表数据
     *
     * @param data
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @PermissionData(pageComponent = "basedata/DepotList")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation("列表查询")
    public Result<IPage<Unit>> queryPageList(Unit data, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<Unit>> result = new Result<IPage<Unit>>();
        QueryWrapper<Unit> queryWrapper = QueryGenerator.initQueryWrapper(data, req.getParameterMap());

        Page<Unit> page = new Page<Unit>(pageNo, pageSize);
        IPage<Unit> pageList = unitService.page(page, queryWrapper);

        result.setSuccess(true);
        result.setResult(pageList);
        log.info(pageList.toString());
        return result;
    }

    /**
     * update
     *
     */
    @DeleteMapping("/delete")
    @ApiOperation("删除")
    public Result<?> delete(@RequestParam(required = true) Serializable id) {
        boolean b = unitService.removeById(id);
        if(b){
            return Result.OK().success("删除成功!");
        }else{
            return Result.OK(false);
        }
    }


    /**
     * save
     *
     */
    @RequestMapping(value = "/addX")
    public Result save(@RequestBody Unit data) {
        data.setName(data.getBasicUnit() + "," + data.getOtherUnit() + "(1:" + data.getRatio() + ")");
        boolean b = unitService.save(data);
        if(b){
            return Result.OK(true);
        }else{
            return Result.OK(false);
        }
    }
    /**
     * update
     *
     */
    @RequestMapping("/updateX")
    public Result update(@RequestBody Unit data) {
        data.setName(data.getBasicUnit() + "," + data.getOtherUnit() + "(1:" + data.getRatio() + ")");
        boolean b = unitService.updateById(data);
        if(b){
            return Result.OK(true);
        }else{
            return Result.OK(false);
        }
    }

}
